package com.wang.springbootinit.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.model.dto.teacher.TeacherAddRequest;
import com.wang.springbootinit.model.dto.teacher.TeacherEditRequest;
import com.wang.springbootinit.model.dto.teacher.TeacherQueryRequest;
import com.wang.springbootinit.model.vo.TeacherVO;
import com.wang.springbootinit.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin/teacher")
public class AdminTeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 分页获取教师列表
     */
    @GetMapping("/list")
    public BaseResponse<Page<TeacherVO>> listTeacherByPage(TeacherQueryRequest teacherQueryRequest) {
        if (teacherQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<TeacherVO> teacherPage = teacherService.getTeacherVOPage(teacherQueryRequest);
        return ResultUtils.success(teacherPage);
    }

    /**
     * 获取教师信息
     */
    @GetMapping("/get")
    public BaseResponse<TeacherVO> getTeacherById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TeacherVO teacherVO = teacherService.getTeacherVOById(id);
        return ResultUtils.success(teacherVO);
    }

    /**
     * 添加教师
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeacher(@RequestBody TeacherAddRequest teacherAddRequest, HttpServletRequest request) {
        if (teacherAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long teacherId = teacherService.addTeacher(teacherAddRequest);
        return ResultUtils.success(teacherId);
    }

    /**
     * 更新教师信息
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeacher(@RequestBody TeacherEditRequest teacherEditRequest) {
        if (teacherEditRequest == null || teacherEditRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teacherService.updateTeacher(teacherEditRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除教师
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeacher(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teacherService.removeById(id);
        return ResultUtils.success(result);
    }
}
